#import "BranddetailsViewController.h"
#import "BranddetailCell.h"
#import "DBrandDetailCell.h"
#import "BrandLabelList.h"

#import "merchantList.h"
#import "UIColor+FlatUI.h"
#import "BrandDetailMessage.h"
#import "UIImageView+WebCache.h"
#import "RepairAndHomeViewController.h"
#import "CityCommunityListViewController.h"

NSString *const BranddetailsTableViewCellID = @"BrandoneCell";
NSString *const DBranddetailsTableViewCellID = @"BrandtwoCell";


@interface BranddetailsViewController ()<CSBandCellDelegate>

@property (nonatomic,strong) NSString *brandID;
@property (nonatomic,strong) BrandDetailMessage *brandDetailMessage;

@end

@implementation BranddetailsViewController
@synthesize BranddetailTable;

-(instancetype)initWithBrandID:(NSString *)brandID
{
    self = [super init];
    if (self) {
        self.brandID = brandID;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"品牌详情";
    [self addBackButton];
    [self setUpTableViews];
    [self webRequest_getBrandDetailMessgaeWithBrandID:self.brandID];
}

-(void)webRequest_getBrandDetailMessgaeWithBrandID:(NSString *)brandID
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KInterface_GetBrand,brandID];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            self.brandDetailMessage = [BrandDetailMessage objectFromDictionary:response[@"value"]];
            [self.BranddetailTable reloadData];
            DLog(@"%@",response);
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [BranddetailTable registerNib:[BranddetailCell nib] forCellReuseIdentifier:BranddetailsTableViewCellID];
    [BranddetailTable registerNib:[DBrandDetailCell nib] forCellReuseIdentifier:DBranddetailsTableViewCellID];
}

#pragma mark - TableViewCell delegate 

-(void)senderCellBtnClick:(UIButton *)btn
{
    CityCommunityListViewController *cityCommunityVC = [[CityCommunityListViewController alloc]initWithMerchartID:self.brandID];
    [self.navigationController pushViewController:cityCommunityVC animated:YES];
    
//    RepairAndHomeViewController *merchantListViewController = [[RepairAndHomeViewController alloc] initWithMerchantID:self.brandID];
//    [self.navigationController pushViewController:merchantListViewController animated:true];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
    {
        return 1;
    }else{
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 15;
    }else{
        return 5;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"F1F0E8"]];
    
    return sectionView;
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        BranddetailCell *branddetailCell = [tableView dequeueReusableCellWithIdentifier:BranddetailsTableViewCellID];
        branddetailCell.selectionStyle = UITableViewCellSelectionStyleNone;

        [branddetailCell configWithBrandDetail:self.brandDetailMessage];
        return branddetailCell;
    }else {
        DBrandDetailCell *DbranddetailCell = [tableView dequeueReusableCellWithIdentifier:DBranddetailsTableViewCellID];
        DbranddetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
        DbranddetailCell.delegate = self;
        DbranddetailCell.contentLable.text = self.brandDetailMessage.brief;
        [DbranddetailCell.contentImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/400/h/200/m/1",self.brandDetailMessage.image]] placeholderImage:[UIImage imageNamed:@"品牌详情_主图"]];
        return DbranddetailCell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return [BranddetailCell heightOfTableCell];
    }else{
        return [DBrandDetailCell heightOfTableCell];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
